"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : GUC_ErrorLog
Case Name   : 设置log_min_messages = debug5 时，执行特定create table及select操作正常
Create At   : 2023/2/18
Owner       : @peilinqian
Description :
    1、设置log_min_messages为debug5;
    2、创建表并插入数据;
    3、执行create table子句包含as.. union all ...关键字;
    4、执行select语句，where子句包含NULL IN或‘’ IN;
Expect      :
    1、设置log_min_messages为debug5; expect:修改成功
    2、创建表并插入数据; expect：成功
    3、执行create table子句包含as.. union all ...关键字; expect：成功
    4、执行select语句，where子句包含NULL IN或‘’ IN; expect：成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class ErrorLog(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:start----')
        self.pri_dbuser = Node('PrimaryDbUser')
        self.pri_root = Node('PrimaryRoot')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.com = Common()
        self.constant = Constant()
        self.t_name1 = 't_guc_errorlog_0133_1'
        self.t_name2 = 't_guc_errorlog_0133_2'

    def test_main(self):
        step_txt = '----step0:查看log_min_messages初始配置值;----'
        self.log.info(step_txt)
        self.init_para = self.com.show_param("log_min_messages")

        step_txt = '----step1:设置log_min_messages为debug5; expect:修改成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execute_gsguc('reload',
                                           self.constant.GSGUC_SUCCESS_MSG,
                                           f"log_min_messages= debug5")
        self.assertTrue(result, '执行失败:' + step_txt)
        alter_para = self.com.show_param("log_min_messages")
        self.assertEqual(alter_para, 'debug5', '执行失败:' + step_txt)

        step_txt = '----step2:创建表并插入数据; expect：成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name1};" \
            f"create table {self.t_name1}(c_id int,c_text text);" \
            f"insert into {self.t_name1} values(1,'ugfd'),(2,'kjjhbn')," \
            f"(3,'ihgd'),(4,''),(5,NULL);"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn("INSERT 0 5", create_result, '执行失败:' + step_txt)

        step_txt = '----step3:执行create table子句包含as.. union all ...关键字; ' \
                   'expect：成功----'
        self.log.info(step_txt)
        create_sql = f"drop table if exists {self.t_name2};" \
            f"create table {self.t_name2} as " \
            f"(select c_id c1,c_text c2 from {self.t_name1}) " \
            f"union all (select c_id c3,c_text c4 from {self.t_name1});"
        create_result = self.pri_sh.execut_db_sql(create_sql)
        self.log.info(create_result)
        self.assertIn("INSERT 0 10", create_result, '执行失败:' + step_txt)

        step_txt = '----step4:执行select语句，where子句包含NULL IN或‘’ IN; ' \
                   'expect：成功----'
        self.log.info(step_txt)
        select_sql = f"select * from {self.t_name1} where NULL IN " \
            f"(select c_text from {self.t_name1} where c_id >3 );"
        select_result = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(select_result)
        self.assertIn("0 rows", select_result, '执行失败:' + step_txt)

        select_sql = f"select * from {self.t_name1} where '' IN " \
            f"(select c_text from {self.t_name1} where c_id >3);"
        select_result = self.pri_sh.execut_db_sql(select_sql)
        self.log.info(select_result)
        self.assertIn("0 rows", select_result, '执行失败:' + step_txt)

    def tearDown(self):
        self.log.info('----this is teardown----')
        step1_txt = '----删除数据库 expect: 成功----'
        self.log.info(step1_txt)
        drop_sql = f"drop table if exists {self.t_name1},{self.t_name2};"
        drop_result = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(drop_result)
        step2_txt = '----还原参数log_min_messages; expect:修改成功----'
        self.log.info(step2_txt)
        msg2 = self.pri_sh.execute_gsguc('reload',
                                         self.constant.GSGUC_SUCCESS_MSG,
                                         f"log_min_messages= "
                                         f"{self.init_para}")
        alter_para = self.com.show_param("log_min_messages")

        step_txt = '----断言teardown执行成功----'
        self.log.info(step_txt)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, drop_result,
                      '执行失败:' + step1_txt)
        self.assertTrue(msg2, '执行失败:' + step2_txt)
        self.assertEqual(alter_para, self.init_para, '执行失败:' + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:end----')
